import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 和Promise类似
 */
public class TestCompletableFutureThen {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行...");

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            CompletableFuture.supplyAsync(() -> {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        return new Random().nextInt(10);
                    }, executorService)
                    .thenApply(data -> {
                        System.out.println("data:" + data);
                        return data * 100;
                    })
                    .thenAccept(res -> {
                        System.out.println("res:" + res);
                    });
        }
    }


}
